<template>
    <div class="Roal">
        <!--        面包导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!--        卡片-->
        <el-card class="box-card">
            <!--            搜索区域-->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入角色名"  v-model="queryInfo.name" clearable>
                        <!--                        <el-select  slot="prepend" placeholder="请选择" >-->
                        <!--                            <el-option label="餐厅名" value="1"></el-option>-->
                        <!--                            <el-option label="订单号" value="2"></el-option>-->
                        <!--                            <el-option label="用户电话" value="3"></el-option>-->
                        <!--                        </el-select>-->
                        <el-button slot="append" icon="el-icon-search" @click="getRoalList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="dialogVisible = true">添加</el-button>
                </el-col>
            </el-row>
            <!--            表格区域-->
            <el-table :data="roalLists" border>
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-row :class="['bdbottom', il ===0 ? 'bdtop' : '','vcenter']" v-for="(item,il) in scope.row.subMen" :key="item.id">
<!--                            一级权限-->
                            <el-col :span="5">
                                <el-tag>{{item.name}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
<!--                            二级权限-->
                            <el-col :span="19">
                                <el-row :class="['bdbottom', i2 ===0 ? 'bdtop' : '','vcenter']" v-for="(item2,i2) in item.subMen" :key="item2.id">
                                    <el-col :span="6">
                                        <el-tag type="success">{{item2.name}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                    <el-tag type="warning" v-for="item3 in item2.subMen" :key="item3.id"  >
                                        {{item3.name}}
                                    </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名" prop="name"></el-table-column>
                <el-table-column label="角色描述" prop="describeinfo"></el-table-column>
                <el-table-column label="创建时间" prop="createDate"></el-table-column>
                <el-table-column label="操作"  width="180px">
                    <template slot-scope="scope">
                        <!--                            修改-->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
                        <!--                            删除-->
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUser(scope.row.id)"></el-button>
                        <!--                            分配权限-->
                        <el-tooltip effect="dark" content="分配权限" placement="top" :enterable="false" >
                            <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightDialog(scope.row)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!--                分页-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.page"
                    :page-sizes="[1, 5, 10, 50]"
                    :page-size="queryInfo.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>

        <!--        添加框-->
        <el-dialog
                title="添加角色"
                :visible.sync="dialogVisible"
                width="50%" @close="addClose">
            <!--            表单-->
            <el-form :model="addFoem" :rules="addFoemRules" ref="ruleFormRef" label-width="70px" >
                <el-form-item label="角色名" prop="name">
                    <el-input v-model="addFoem.name"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="describeinfo">
                    <el-input v-model="addFoem.describeinfo"></el-input>
                </el-form-item>
            </el-form>
            <!--            按钮-->
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addRoal">确 定</el-button>
  </span>
        </el-dialog>
        <!--        修改框-->
        <el-dialog
                title="修改信息"
                :visible.sync="editDialogVisible"
                width="50%" >
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px" >
                <el-form-item label="角色名" prop="name">
                    <el-input v-model="editForm.name" ></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="describeinfo">
                    <el-input v-model="editForm.describeinfo" ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserInfo">确 定</el-button>
  </span>
        </el-dialog>
<!--        分配权限-->
        <el-dialog
                title="分配权限"
                :visible.sync="showRight"
                width="50%" @close="closeMenu"
                >
<!--            权限树-->
            <el-tree :data="menuData" :props="treeMenu" ref="treeRef" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys"></el-tree>
            <span slot="footer" class="dialog-footer">
    <el-button @click="showRight = false">取 消</el-button>
    <el-button type="primary" @click="addMenu">确 定</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "Roal",
        data(){
            //
            // var chekcEmil = (rule,value,cb) => {
            //     const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/   //邮箱
            //     // const regMobile =/^(0|86|17951)?(13[0-9]{8})$/  //手机号
            //     if(regEmail.test(value)){
            //         return cb();
            //     }
            //     cb(new Error('请输入合法的邮箱'))
            // }
            return{
                //权限列表
                menuData:[],
                //已有的权限
                defKeys:[],
                //定义树
                treeMenu:{
                    label: 'name',
                    children: 'subMen'
                },
                queryInfo:{
                    id: '',
                    name: '',
                    page: 1,
                    size: 5,
                    code: 1
                },
                roalLists: [],
                total: 0,
                //控制添加框
                dialogVisible: false,
                //控制修改框
                editDialogVisible: false,
                //表单数据对象
                addFoem: {
                    name: '',
                    describeinfo: '',
                    token: ''
                },
                //查询到的用户数据
                editForm: {},
                //表单验证规则
                addFoemRules:{
                    name:[
                        { required: true,message: '请输入用户名',trigger: 'blur'},
                        { min: 1, max: 10,message: '请输入1到10之间',trigger: 'blur'}
                    ],
                    describeinfo:[
                         {  max: 100,message: '不能超过100个字符',trigger: 'blur'}
                ]
                },
                editFormRules:{
                    name:[
                        { required: true,message: '请输入用户名',trigger: 'blur'},
                        { min: 1, max: 10,message: '请输入1到10之间',trigger: 'blur'}
                    ],
                    describeinfo:[
                        {  max: 100,message: '不能超过100个字符',trigger: 'blur'}
                    ]
                },
                isClassname: true,
                //分配权限框
                showRight: false,
                //角色id
                roaliD: ''
            }
        },
        created() {
            this.getRoalList()
        },
        methods:{
            //获取角色列表
            async getRoalList(){
                const {data: res} =await this.$http.get('Roal/getAllRoalList',{
                    params: this.queryInfo
                })
                if(res.code != 200) return this.$message.error('数据获取失败')
                this.roalLists = res.data.data
                this.total = res.data.total
            },
            //监听pagesize的改变
            handleSizeChange(newSize){
                this.queryInfo.size = newSize
                this.getRoalList()
            },
            //页码值改变
            handleCurrentChange(newPage){
                this.queryInfo.page = newPage
                this.getRoalList()
            },
            //关闭表单并重置
            addClose(){
                this.isClassname = true
                this.$refs.ruleFormRef.resetFields()
            },
            //添加用户
            addRoal(){
                this.$refs.ruleFormRef.validate(async valid => {
                    if(!valid) return
                    //添加角色
                    this.addFoem.token = window.sessionStorage.getItem('token')
                    const { data: res} = await this.$http.post('Roal/insertRoal',this.addFoem)
                    if(res.code !== 0) {
                        return this.$message.error(res.message)
                    }else {
                        this.$message.success('添加成功');
                    }

                    //关闭窗口
                    this.dialogVisible = false;
                    //刷新
                    this.getRoalList();
                })
            },
            //展示编辑框
            async showEditDialog(id){
                this.queryInfo.id = id
                this.queryInfo.code = 2
                const {data: res} =await this.$http.get('Roal/getAllRoalList',{
                    params: this.queryInfo
                })
                if(res.code != 200) return this.$message.error('查询数据失败')
                this.editForm = res.data
                this.editDialogVisible = true;
            },
            //修改角色信息
            editUserInfo(){
                this.$refs.editFormRef.validate(async valid => {
                    if(!valid) return
                    //修改用户信息
                    const {data: res} = await this.$http.post('Roal/updateRoal',this.editForm)
                    if(res.code !== 0) return this.$message.error(res.message)
                    this.$message.success(res.message)

                    //关闭修改框
                    this.editDialogVisible = false
                    this.queryInfo.code = 1
                    this.queryInfo.id = ''
                    this.getRoalList()
                })
            },
            //删除用户
            async removeUser(id){
                const confirm = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err =>{
                    return err
                })
                if(confirm !== 'confirm') return this.$message.info('已取消删除')

                const {data:res} = await this.$http.get('Roal/deleteRoal',{
                    params:{
                        id: id
                    }
                })
                this.$message.success(res.message)
                this.getRoalList()
            },
            //删除权限
            // async removeByid(rid,mid){
            //     console.log(rid.id +'aaa'+ mid)
            //     const confirm = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).catch(err => err)
            //     if(confirm !== 'confirm'){
            //         return this.$message.info('取消删除')
            //     }
            // }
            //获取权限列表
           async showSetRightDialog(roal){
                 const {data: res} = await this.$http.get('Menu/getAllMenuTree',{
                    params:{}
                })
               this.menuData = res.data
               //获取已有权限
               this.getLeafKeys(roal.subMen,this.defKeys)
               //获取角色id
               this.roaliD = roal.id
                this.showRight = true
            },
            //递归
            getLeafKeys(node,arr){
                node.forEach(item =>{
                    if(item.subMen.length >0){
                        this.getLeafKeys(item.subMen,arr)
                    }else {
                        return arr.push(item.id)
                    }
                })


            },
            //监听权限框的关闭事件
            closeMenu(){
                this.defKeys = []
            },
            //分配权限
            async addMenu(){
                const keys = [...this.$refs.treeRef.getCheckedKeys(),...this.$refs.treeRef.getHalfCheckedKeys()]
                //设置权限
                const {data: res} = await this.$http.post('roalMenu/insertRoalMenu',{
                        rid: this.roaliD,
                        mids: keys
                })
                if(res.code !==0){
                    return this.$message.error('分配权限失败')
                }
                this.$message.success(res.message)
                this.getRoalList()
                this.showRight = false
            }
        }
    }
</script>

<style lang="less" scoped>
    .box-card{
        box-shadow: 0 1px 1px rgba(0,0,0,0.15) !important;
    }
    .el-table{
        margin-top: 15px;
        font-size: 12px;
    }
    .el-tag{
        margin: 7px;
    }
    .bdtop{
        border-top: 1px solid #eee;
    }

    .bdbottom{
        border-bottom: 1px solid #eee;
    }
    .vcenter{
        display: flex;
        align-items: center;
    }
</style>